package com.wanlong.admin.controller.system;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.wanlong.common.result.PageResult;
import com.wanlong.common.result.VueResult;
import com.wanlong.rpc.service.SysMemberManagerService;
import com.wanlong.sys.model.SysUser;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 万龙
 * @name
 * @project_name wanlong
 * @package_name com.wanlong.admin.controller.system
 * @description
 * @date 2019/2/13
 */

@Controller
@CrossOrigin(origins = "*", maxAge = 3600)//跨域请求
@RequestMapping(value = "/admin/systemUser")
public class SystemController {
    private static ObjectMapper MAPPER = new ObjectMapper();


    @Autowired
    public SysMemberManagerService sysMemberManagerService;

    @RequestMapping(value = "/userList" , method = RequestMethod.GET)//,produces = "application/json;charset=UTF-8"
    @ResponseBody
    public VueResult userList(@RequestParam("loginName")String loginName, @RequestParam("page")Integer page, @RequestParam("limit")Integer limit){//@RequestBody JSONObject json
        PageInfo<SysUser> sysUserList = sysMemberManagerService.getPageSysUserListByName(loginName,page,limit);
        List<SysUser> ul = sysUserList.getList();
        for(SysUser u : ul){
            u.setRole(sysMemberManagerService.getRolesByUser(u.getId()));
        }
        PageResult pageResult = new PageResult();
        pageResult.setItems(ul);
        pageResult.setTotal(sysUserList.getTotal());
//        Map<String ,Object> map = new HashMap<>();
//        map.put("items",ul);
//        map.put("total",sysUserList.getTotal());
//        String result="";
//        try {
//            result= MAPPER.writeValueAsString(map);
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//        }
        return VueResult.success(pageResult);
    }

    @RequestMapping(value = "/createSysUser" , method = RequestMethod.POST)
    @ResponseBody
    public VueResult createSysUser(@RequestBody SysUser sysUser){
        String result=sysMemberManagerService.insertSysUser(sysUser)+"";
        return VueResult.success(result);
    }

    @RequestMapping(value = "/updateSysUser" , method = RequestMethod.POST)
    @ResponseBody
    public VueResult updateSysUser(@RequestBody SysUser sysUser){
        sysUser.setGmtModified(new Date());
        String result=sysMemberManagerService.updateSysUser(sysUser)+"";

        return VueResult.success();
    }

    @RequestMapping(value = "/deleteSysUser" , method = RequestMethod.POST)
    @ResponseBody
    public VueResult deleteSysUser(@RequestBody SysUser sysUser){
        sysUser.setGmtModified(new Date());

        String result=sysMemberManagerService.deleteSysUser(sysUser)+"";

        return VueResult.success();
    }



}
